package org.teachertools.web.actions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.data.Note;
import org.teachertools.services.NoteService;
import org.teachertools.web.forms.NoteForm;
import org.teachertools.web.forms.NoteFormMapping;

public class NoteAction extends CommonDispatchAction {
    @Override
    protected Map<String, String> getKeyMethodMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("button.save", "save");
        map.put("button.delete", "delete");
        map.put("button.search", "search");
        return map;
    }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        NoteForm noteForm = (NoteForm) form;
        NoteService noteService = this.getServiceLocator().getNoteService();
        boolean isUpdate = noteForm.getIsUpdate().booleanValue();
        if (isUpdate) {
            Note loadedNote = noteService.get(noteForm.getId());
            Note note = NoteFormMapping.mappingFormToDTO(noteForm, loadedNote, request);
            noteService.update(note);
        } else {
            Note note = NoteFormMapping.mappingFormToDTO(noteForm, request);
            noteService.save(note);
        }

        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lenregistrement")));
        saveMessages(request, actionMessages);
        noteForm.setId(null);
        noteForm.setNote(null);
        noteForm.setCommentaire(null);

        addClassList(request);
        addEleveList(request);

        addCategoryList(request);
        Long idCategorie = noteForm.getIdCategorie();
        request.setAttribute("idCategorie", idCategorie);
        addMatiereList(request);
        Long idMatiere = noteForm.getIdMatiere();
        request.setAttribute("idMatiere", idMatiere);
        addCompetenceList(request);

        return mapping.findForward("successSave");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        NoteForm noteForm = (NoteForm) form;
        NoteService noteService = this.getServiceLocator().getNoteService();
        Long id = noteForm.getId();
        noteService.deleteById(id);
        noteForm.reset();
        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lasuppression")));
        saveMessages(request, actionMessages);

        addCategoryList(request);
        addClassList(request);

        return mapping.findForward("successDelete");
    }

    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        NoteForm noteForm = (NoteForm) form;
        Note note = NoteFormMapping.mappingFormToDTO(noteForm, request);
        NoteService noteService = this.getServiceLocator().getNoteService();
        List<Note> list = noteService.searchByExample(note);
        request.setAttribute("RESULTS_LIST", list);

        if (list.size() == noteService.getMaxResults()) {
            ActionMessages actionMessages = new ActionMessages();
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("libelle.maxResults", new Integer(
                    noteService.getMaxResults())));
            saveMessages(request, actionMessages);
        }

        addCategoryList(request);
        addMatiereList(request);
        addCompetenceList(request);

        addClassList(request);
        addEleveList(request);

        return mapping.findForward("successSearch");
    }
}
